package com.meowcook.controller;

import com.meowcook.common.AppResult;
import com.meowcook.config.AppConfig;
import com.meowcook.pojo.dto.CollectionDTO;
import com.meowcook.pojo.vo.CollectionVO;
import com.meowcook.pojo.vo.UserPartIVO;
import com.meowcook.service.ICollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-05-16
 * Time: 15:43
 */
@Slf4j
@Api(tags = "食谱收藏操作接口")
@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Resource
    private ICollectionService collectionService;

    @PostMapping("/collect")
    @ApiOperation("食谱(取消)收藏")
    public AppResult collect(HttpServletRequest request, @Valid @RequestBody CollectionDTO collection) {
        log.info("收藏操作: {}", collection);
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        Long userId = user.getId();
        collection.setUserId(userId);
        collectionService.collect(collection);
        return AppResult.success("收藏成功");
    }

    @GetMapping("/getCollectionRecord")
    @ApiOperation("查询登录用户对指定食谱的收藏记录")
    public AppResult<CollectionVO> getCollectionRecord(HttpServletRequest request,
                                           @ApiParam("食谱id") @RequestParam("recipeId") @NonNull Long recipeId) {
        log.info("查询食谱id: {}", recipeId);
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        CollectionVO collectionVO = collectionService.selectCollectionRecord(user.getId(), recipeId);
        return AppResult.success(collectionVO);
    }
}
